<?php 
	$pPageIsPublic = false;
	include '../_common.php';
	$_SESSION["m1"]="destination";
	$_SESSION["m2"]="destination-list";
	
	
	$pTitle  = 'New Country';
	$pButton = 'Save';
	$action  = 'add';
	$id 	 = intval($_REQUEST['id']);
	$latLng	 = NULL;
	
	
	
	
	$objCountry = new Country();
	$objCountry ->setUid($id);
	if ($objCountry ->load()) {
		$pTitle  = 'Edit Country';
		$pButton = 'Update';
		$action  = 'edit';
		$latLng	 = json_decode($objCountry ->locationMap);
	}
	
	if ($_GET['action']== 'del') {
		if ($objCountry ->isLoaded()) {
			switch ($_GET['i']){
				case "h" :
					$objCountry ->removeImage('bnImage');
					$msg = "Banner Home deleted.";
					break;
				case "b" :
					$objCountry ->removeImage('bgImage');
					$msg = "Picture Page deleted.";
					break;
			}
		}
	}
	
	if (isset($_POST['submit'])) {
		$objCountry ->setAuto($_POST);
		$objCountry ->locationMap = arr2json($_POST, array('lati', 'long', 'zoom'));
		
		$objCountry ->uploadImage('bnImage', 'bnImage');
		$objCountry ->uploadImage('bgImage', 'bgImage');
		
		if ($_POST['action'] == 'edit') {
			$objCountry ->getConnection();
			$objCountry ->setUid($id);
			$objCountry ->update();
			Tzn::redirect($_SERVER['PHP_SELF']."?id=". $objCountry ->id, 'Country has been updated.');
		} elseif ($_POST['action'] == 'add') {
			$objCountry ->add();
			Tzn::redirect($_SERVER['PHP_SELF']."?id=". $objCountry ->id, 'Country has been added.');
		}
	}

?>
<?php include("header.php"); ?>
<?php
$tabs 	= array('region' => 1);
$selTab = $tabs[$_REQUEST['tab']] ? $tabs[$_REQUEST['tab']] : 0;
?>
<script type="text/javascript">
$(document).ready(function(){
	$('#frmMain').validate();
	$('#content-tabs').tabs({
		disabled :[<?php echo !$objCountry ->isLoaded() ? "2" : "" ?>],
		initial  : <?php echo $selTab ?>
	});

	$('ul#listin').admin_sortable("Region");
	$('#select-all').checkedToggle('ul#listin');
	
	$('#del').live('click', function(){
		var check = $(this).closest("li").find('input:checkbox');  
		check.attr('checked', true);
		delItemSelected(check);
	});		
});

function sendForm($frm){
	if ($frm.valid()) {
		$('input[name="lati"]').val($('span#lat').text());
		$('input[name="long"]').val($('span#lng').text());
		return true;
	}
	return false;
}




function delItemSelected(btn) {
	var checked = $('ul#listin').find('input:checked');
	
	if (checked.length == 0) {
		Notify('Select a Item.!',1);
		return false;
	} else {
		var items, li;
		if (btn.id == "checked") {
			items = checked.serialize();
			li = checked.closest("li");
		} else {
			items = $(btn).serialize();
			li = $(btn).closest("li");
		}
		
		items = decodeURIComponent(items);
		
		if (! confirm("You going to delete "+ li.length +" items ?")){
			return false;
		} else {
			ajaxAdmin({
				data: items + "&action=item-delete&class=region",
				success: function(response) {
					if (response) {
						Notify(response, 0);
						li.fadeOut(300, function(){$(this).remove();});
					}	
				}
			});
		}					
	}
}

</script>
<?php include("sidebar.php"); ?>
<ul id="subnav" class="bar">
	<li ><a href="destination-list.php" > Country List </a></li>
	<li><font>&raquo;</font></li>
	<li class="noborder" ><span><?php echo $pTitle;?> </span></li>
</ul>
<div id="list">
	<h2> <?php echo $pTitle;?> </h2>
    <div id="content-tabs">
		<ul>
			<li><a href="#fragment-1"><span>Country Details</span></a></li>
			<li><a href="#fragment-2"><span>Regions</span></a></li>
		</ul>
		<div id="fragment-1" >
			<form action="" method="post" name = "frmMain" id = "frmMain" enctype="multipart/form-data" onsubmit="return sendForm($('#frmMain'));" >
			<fieldset><legend> General Info </legend>
					<table cellpadding="0" cellspacing="0" class="table-form"  >
						<tr>
							<th width="12%"  >Continent: </th>
							<td width="88%" ><?php
							$tmp = new Continent();
							 $tmp ->addOrder('title ASC');
							Generic::gSelect($tmp, '', $objCountry ->continent ->id, '--- select a continent ---', '', 'class=""');    ?></td>
						</tr>
						<tr>
							<th>Title: </th>
							<td><?php $objCountry ->qText('title', '', 'width:548px', 'class="required"');?></td>
						</tr>
						<!--tr>
							<th>Sub Title: </th>
							<td><?php $objCountry ->qText('subTitle', '', 'width:548px', 'class=""');?></td>
						</tr-->
						<tr><td colspan="2" >
						<br/>
							<label>Description: </label>
						<?php $objCountry ->qEditor('description', '', $config ); ?></td>
						</tr>
<tr><td colspan="2" >

 <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAbFpq9FKh1m9AsL17lJO3mhQNhyHe8Ap_qBXlY1vIBbwSz7cwzRQ18Yxk2lVIhL5v4ljzQh45FvvFzA" type="text/javascript"></script>
 
<script type="text/javascript">

 function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        var center = new GLatLng(<?php echo ($latLng ->lati && $latLng ->long)  ? $latLng ->lati.", ".$latLng ->long : "25.78897, -80.22644" ?>);
        map.setCenter(center, <?php echo ($latLng ->zoom)  ? $latLng ->zoom : "5" ?> );
        geocoder = new GClientGeocoder();
        var marker = new GMarker(center, {draggable: true});  
        map.addOverlay(marker);

<?php #if ($objCountry ->isLoaded()):  ?>
        document.getElementById("lat").innerHTML = center.lat().toFixed(5);
        document.getElementById("lng").innerHTML = center.lng().toFixed(5);
        document.getElementById("zoom").value = map.getZoom();
<?php # endif;?>        

	  GEvent.addListener(marker, "dragend", function() {
       var point = marker.getPoint();
	      map.panTo(point);
       document.getElementById("lat").innerHTML = point.lat().toFixed(5);
       document.getElementById("lng").innerHTML = point.lng().toFixed(5);

        });


	 GEvent.addListener(map, "moveend", function() {
	//	  map.clearOverlays();
    var center = map.getCenter();
		  var marker = new GMarker(center, {draggable: true});
//		  map.addOverlay(marker);
		  document.getElementById("lat").innerHTML = center.lat().toFixed(5);
	   document.getElementById("lng").innerHTML = center.lng().toFixed(5);


	 GEvent.addListener(marker, "dragend", function() {
      var point =marker.getPoint();
	     map.panTo(point);
      document.getElementById("lat").innerHTML = point.lat().toFixed(5);
	     document.getElementById("lng").innerHTML = point.lng().toFixed(5);

        });
 
        });

	 GEvent.addListener(map, "zoomend", function(oldzoom,zoom) {
		  //document.getElementById("zoom").innerHTML=zoom;
		  //alert(zoom);
		 document.getElementById("zoom").value = zoom;
		}); 

      }
    }

	   function showAddress(address) {
	   var map = new GMap2(document.getElementById("map"));
       map.addControl(new GSmallMapControl());
       map.addControl(new GMapTypeControl());
       if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
		  document.getElementById("lat").innerHTML = point.lat().toFixed(5);
	   document.getElementById("lng").innerHTML = point.lng().toFixed(5);
		 map.clearOverlays()
			map.setCenter(point, <?php echo ($latLng ->zoom)  ? $latLng ->zoom : "5" ?>);
   var marker = new GMarker(point, {draggable: true});  
		 map.addOverlay(marker);

		GEvent.addListener(marker, "dragend", function() {
      var pt = marker.getPoint();
	     map.panTo(pt);
      document.getElementById("lat").innerHTML = pt.lat().toFixed(5);
	     document.getElementById("lng").innerHTML = pt.lng().toFixed(5);
        });


	 GEvent.addListener(map, "moveend", function() {
//		  map.clearOverlays();
    var center = map.getCenter();
		  var marker = new GMarker(center, {draggable: true});
//		  map.addOverlay(marker);
		  document.getElementById("lat").innerHTML = center.lat().toFixed(5);
	   document.getElementById("lng").innerHTML = center.lng().toFixed(5);

	 GEvent.addListener(marker, "dragend", function() {
     var pt = marker.getPoint();
	    map.panTo(pt);
    	document.getElementById("lat").innerHTML = pt.lat().toFixed(5);
	   document.getElementById("lng").innerHTML = pt.lng().toFixed(5);
        });
 
        });

	 GEvent.addListener(map, "zoomend", function(oldzoom,zoom) {
		  //document.getElementById("zoom").innerHTML=zoom;
		  //alert(zoom);
		 document.getElementById("zoom").value = zoom;
		}); 

            }
          }
        );
      }
    }

$(window).load(function(){
});

$().ready(function(){

	load();
	GUnload();
	
	$('input#search').click(function(){
		showAddress($(this).prev().val());
	});

	//$('input#search').trigger('click');	
});


</script>

 <p>        
      <input onkeydown="javascript:if (event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('search').click(); return false;}};"
           type="text" size="60" name="address" value="" />
      <input type="button" id="search"  value="Search" name="search" />
 </p>
 <p>
 	<b>Latitude: </b><span id="lat" ></span><input type="hidden" name="lati"  /><br/>
 	<b>Longitude: </b><span id="lng" ></span><input type="hidden" name="long"  /><input type="hidden" name="zoom" id="zoom"  />
 	<?php
 		if ($_REQUEST['tab'])
 			echo '<a  onclick="load();" href="javascript:void(0)" style="float:right; margin-right:12px; font-size:12px;" > fix size </a>'; 
 	?>
 	
 </p>

 <div align="center" id="map" style="width: 648px; height: 400px"><br/></div>

</td>
	
</tr>						
						<?php $objCountry ->printSEO()  ?>
						<tr>
							<th>Home pic: </th>
							<td>
						<?php	if ($objCountry ->bnImage): ?>
								<div style="width: 230px;" class="img-border">
									<img alt="" src="../thumb.php?src=<?php echo TZN_FILE_UPLOAD_URL_ADMIN.$objCountry ->bnImage?>&w=230&h=80" width="230" height="80"   ><br/>
									<a href="<?php echo $_SERVER['PHP_SELF']  ?>?action=del&i=h&id=<?php echo $objCountry ->id ?>"  >delete image</a>
								</div>
							<?php
								else:
									$objCountry ->qFile('bnImage', '', '', 'class=""'); 		
								endif; 
							?>
							</td>
						</tr>
						<tr>
							<th>Page pic: </th>
							<td>
						<?php	if ($objCountry ->bgImage): ?>
								<div style="width: 230px;" class="img-border">
									<img alt="" src="../thumb.php?src=<?php echo TZN_FILE_UPLOAD_URL_ADMIN.$objCountry ->bgImage?>&w=230&h=80" height="80" width="230"  ><br/>
									<a href="<?php echo $_SERVER['PHP_SELF']  ?>?action=del&i=b&id=<?php echo $objCountry ->id ?>"  >delete image</a>
								</div>
							<?php
								else:
									$objCountry ->qFile('bgImage', '', '', 'class=""'); 		
								endif; 
							?>
							</td>
						</tr>
						<tr>
							<th>Status: </th>
							<td><?php $objCountry ->arrayToSelect($objCountry ->_arrStatus, 'status', $objCountry ->status, false, 'width:100px', '');?></td>
						</tr>
					</table>
			</fieldset>
			<div style="margin-top:10px">
				<input type="hidden" name="id" value="<?php echo $id; ?>" />
				<input type="hidden" name="action" value="<?php echo $action; ?>" />
                <input type="submit" name="submit"  value="<?php echo $pButton; ?>" /> &nbsp; <input type="reset" value="Cancel" onClick="window.location.href='destination-list.php'" />
            </div>
            </form>
		</div>  
		<div id="fragment-2" >
		<ul id="subnav">
 			<li><input type="checkbox" name="select-all" id="select-all" /><label for="select-all" class="first-check" >Select All</label></li>
			<li class="" ><a href="javascript:void(0);" id="checked" onclick="delItemSelected(this)"  >Delete Selection</a></li>
<?php
	if ($objCountry ->isLoaded()): 
?>			
			<li class="noborder"  >
				<a class="add"  href="region-add.php?id=<?php echo $objCountry ->id; ?>" > Add a New Region </a>
			</li>
<?php
	endif; 
?>
		</ul>
		<table id="gradient-style" >
			<thead>
				<tr>
		    		<th width="80%" >Region</th>
		    		<th width="20%" >Action</th>
		  		</tr>
			</thead>
		</table>
		<ul id="listin">
		<?php
		$objRegion = new Region();
		$objRegion ->loadItems(null, "#status like '".$chkstatus."%' AND #countryId=". $objCountry ->id);
		while($objItem = $objRegion ->rNext()):
		?>
			<li id="recordsArray_<?php echo $objItem->id; ?>" ><table id="gradient-style">	
				<tr>
					<td width="4%" ><input type="checkbox" name="items[]" value="<?php echo $objItem->id;?>" /></td>
		    		<td width="76%" ><strong><?php echo $objItem ->title  ?></strong>
			    		<p><?php echo truncate_string(strip_tags($objItem ->description),160) ?></p>
			    		<strong class="note-small" >status: </strong><?php echo $objItem ->status  ?><br/>
			    		<p class="subnav">
			    			<a href="region-add.php?id=<?php echo $objItem ->country ->id ?>&rid=<?php echo $objItem ->id ?>" > edit  </a>
			    			<a href="javascript:void(0)" id="del" > delete  </a>
			    		</p>
		    		</td>
		    		<td width="20%" >
		    			<table>
						  <tr>
						    <th>Diving Center:</th>
						    <td><a href="divingcenter-add.php?id=<?php echo $objItem ->country ->id ?>&rid=<?php echo $objItem ->id ?>" >new</a><a href="region-add.php?id=<?php echo $objItem ->country ->id ?>&rid=<?php echo $objItem ->id ?>&tab=divcenter" >list</a></td>
						  </tr>
						  <tr>
						    <th>Accomodation:</th>
						    <td><a href="accomodation-add.php?id=<?php echo $objItem ->country ->id ?>&rid=<?php echo $objItem ->id ?>" >new</a><a href="region-add.php?id=<?php echo $objItem ->country ->id ?>&rid=<?php echo $objItem ->id ?>&tab=accomodation" >list</a></td>
						  </tr>
						</table>    		
		    		</td>
		  		</tr></table>
		  	</li>
		<?php endwhile;  ?>  		
			</ul>
		</div> 
	</div>
</div>
<?php include("footer.php"); ?>